package com.team.hiring.common.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.common.Constants;
import com.team.hiring.common.dao.ApplicationDao;
import com.team.hiring.common.entity.ApplicationEntity;
import com.team.hiring.common.entity.PageModel;
import com.team.hiring.company.entity.CompanyEntity;

/**
 * Servlet implementation class ApplicationList
 */
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    ApplicationDao applicationDao = new ApplicationDao();   
    
    public ApplicationServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("application".equals(command)) {
			application(request, response);
		}else if("updateStatus".equals(command)){
			updateStatus(request, response);
		}else if("apply".equals(command)){
			apply(request, response);
		}else if("applicationResume".equals(command)){
			applicationResume(request, response);
		}else if("queryApplication".equals(command)){
			queryApplication(request, response);
		}
	}
	
	
	/**
	 * 查询申请表信息
	 * @param request
	 * @param response
	 */
	private void application(HttpServletRequest request,
			HttpServletResponse response) {
		CompanyEntity companyEntity = (CompanyEntity) request.getSession()
				.getAttribute("companyEntity");
		int companyId = companyEntity.getId();
		List<ApplicationEntity> applicationEntity = applicationDao.application(companyId);
		request.setAttribute("applicationEntity", applicationEntity);
		try {
			request.getRequestDispatcher("/WEB-INF/view/company/companymain.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 * 异步修改求审核状态
	 * @param request
	 * @param response
	 */
	private void updateStatus(HttpServletRequest request,
			HttpServletResponse response) {
		int applicationId = Integer.parseInt(request.getParameter("applicationId"));
		int status = Integer.parseInt(request.getParameter("status"));
		int result = applicationDao.updateStatus(applicationId,status);
		if (result !=0) {
			try {
				response.getWriter().print("success");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	/**
	 * 投递简历
	 * @param request
	 * @param response
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response) {
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		int row = applicationDao.apply(resumeId,positionId,talentId);
		if(row!=0){
				try {
					response.sendRedirect("/WEB-INF/view/common/positionDetailInfo.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
	}
	
	

	
	/**
	 * 申请职位
	 * @param request
	 * @param response
	 */
	private void applicationResume(HttpServletRequest request,
			HttpServletResponse response) {
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		int resumeId = Integer.parseInt(request.getParameter("aaa"));
		int row = applicationDao.applicationResume(positionId,talentId,resumeId);
		if(row !=0){
			try {
				request.getRequestDispatcher("PositionServlet?command=homepagePositionDateilInfo&positionId="+positionId).forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	
	
	/**
	 * 求职者查看申请状态
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void queryApplication(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		        //获取参数
				int talentId = Integer.parseInt(request.getParameter("talentId"));
				int pageNo = Constants.DEFUALT_PageNo;
				int pageSize = Constants.DEFAULT_PAGE_SIZE;
				//调用业务逻辑
				if(pageNo != 0 &&pageSize != 0){
				PageModel<ApplicationEntity> pageModel = applicationDao.applicationTalent(talentId,pageNo,pageSize);
				//转向
				request.getSession().setAttribute("pageModel", pageModel);
				request.getRequestDispatcher("/WEB-INF/view/talent/talent-main.jsp").forward(request, response);
			}
	}
	
}
